/**
 * Copyright (c) 2006-2016 Huize Ltd. All Rights Reserved. 
 *  
 * This code is the confidential and proprietary information of   
 * Hzins. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Huize,http://www.huize.com.
 *  
 */
package com.agan.concurrent;

import java.io.IOException;
import java.net.Socket;

/**
 * @author hz1412988
 * @date 2018年2月8日 下午5:40:39
 * @version
 */
public class ReaderThread extends Thread {

    private static final int BUFSZ = 512;

    private final Socket socket;

    private final InputStream in;

    public ReaderThread(Socket socket) throws IOException {
	this.socket = socket;
	this.in = socket.getInputStream();
    }

    public void interrupt() {
	try {
	    // 关闭套接字
	    // 此时in.read会抛出异常
	    socket.close();
	} catch (IOException ignored) {
	} finally {
	    // 正常的中断
	    super.interrupt();
	}
    }

    public void run() {
	try {
	    byte[] buf = new byte[BUFSZ];
	    while (true) {
		int count = in.read(buf);
		if (count < 0)
		    break;
		else if (count > 0)
		    processBuffer(buf, count);
	    }
	} catch (IOException e) {
	    // 如果socket关闭，in.read方法将会抛出异常
	    // 借此机会，响应中断，线程退出
	}
    }

    public void processBuffer(byte[] buf, int count) {
    }
}
